package com.shushan.home.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.shushan.home.bean.RecordBean;
import com.shushan.home.bean.TotalPlayBean;
import com.shushan.shushanbao.R;
import com.shushan.util.ToolUtils;

import java.util.List;

/**
 * @ClassName: TotalUseTimeAdapter
 * @Desciption: //总获得使用时间下方的APP使用时间适配器
 * @author: zhangshihao
 * @date: 2018-09-28
 */
public class TotalUseTimeAdapter extends RecyclerView.Adapter<TotalUseTimeAdapter.ItemViewHolder>{

    private double allPlayTime = 0;
    private List<TotalPlayBean.TotalPlayListBean> totalPlayListBeans;

    public TotalUseTimeAdapter(List<TotalPlayBean.TotalPlayListBean> totalPlayListBeans,double allPlayTime){
        this.totalPlayListBeans = totalPlayListBeans;
        this.allPlayTime = allPlayTime;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder holder = new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_study_detail_app_use_time,null));
        return holder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        if(totalPlayListBeans == null){
            return;
        }

        TotalPlayBean.TotalPlayListBean listBean = totalPlayListBeans.get(position);
        if(allPlayTime > 0){
            int percent = (int) (listBean.num/(allPlayTime*60)*100);
            holder.progressBar.setProgress(percent);
        }else{
            holder.progressBar.setProgress(0);
        }
        holder.tvAppName.setText(ToolUtils.getPreThreeStr(listBean.app_name));
        int time = 0;
        double useTime = listBean.num/60;
        time = (int)useTime;
        holder.tvUseTime.setText(time+"分钟");
        if(position == (totalPlayListBeans.size() - 1)){
            holder.viewDivider.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return totalPlayListBeans.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView tvAppName;
        private ProgressBar progressBar;
        private TextView tvUseTime;
        private View viewDivider;
        public ItemViewHolder(View itemView) {
            super(itemView);
            tvAppName = itemView.findViewById(R.id.tv_app_name);
            progressBar = itemView.findViewById(R.id.progressBar);
            tvUseTime = itemView.findViewById(R.id.tv_use_time);
            viewDivider = itemView.findViewById(R.id.v_divider);
        }
    }
}
